//    Dhammapada PSP
//    Copyright (C) 2007 Sakya
//    sakya_tg@yahoo.it
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program; if not, write to the Free Software
//    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#include "menu.h"

OSL_FONT *fontNormal;
OSL_FONT *fontSelected;
OSL_FONT *fontHelp;
OSL_IMAGE *pointer;

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// initMenu: loads fonts
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int initMenu(struct settings *userSettings){
    char buffer[262];
    sprintf(buffer, "%s/images/font.oft", userSettings->ebootPath);
    fontNormal = oslLoadFontFile(buffer);
    if (!fontNormal)
        errorLoadImage(buffer);
    sprintf(buffer, "%s/images/fontSel.oft", userSettings->ebootPath);
    fontSelected = oslLoadFontFile(buffer);
    if (!fontSelected)
        errorLoadImage(buffer);
    sprintf(buffer, "%s/images/fontHelp.oft", userSettings->ebootPath);
    fontHelp = oslLoadFontFile(buffer);
    if (!fontHelp)
        errorLoadImage(buffer);
        
    sprintf(buffer, "%s/images/pointer.png", userSettings->ebootPath);
    pointer = oslLoadImageFilePNG(buffer, OSL_IN_RAM | OSL_SWIZZLED, OSL_PF_8888);
    if (!pointer)
        errorLoadImage(buffer);
    oslSetImageRotCenter(pointer);

    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// clearMenu: clear menu data:
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int clearMenu(struct menuElements *menu){
    int i;
    
    menu->selected = 0;
    menu->first = 0;
    menu->maxNumberVisible = 0;
    menu->yPos = 0;
    menu->xPos = 0;
    menu->width = 0;
    menu->height =  0;
    if (menu->background)
        oslDeleteImage(menu->background);
    for (i=0; i < menu->numberOfElements; i++){
        strcpy(menu->elements[i].name, "");
        strcpy(menu->elements[i].text, "");
        strcpy(menu->elements[i].helpText, "");
        menu->elements[i].triggerFunction = NULL;
    }
    menu->numberOfElements = 0;
    menu->interline = 0;
    menu->cancelFunction = NULL;
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// drawMenu: draws a menu on video
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int drawMenu(struct menuElements *menu){
    int i = 0;
    int count = 0;
    int xPos = 0;
    int yPos = 0;
    
    int startY = menu->yPos + (float)(menu->height -  menu->maxNumberVisible * (fontNormal->charHeight + menu->interline)) / 2.0;
    if (menu->background != NULL)
        oslDrawImageXY(menu->background, menu->xPos, menu->yPos);
        
    for (i=menu->first; i<menu->first + menu->maxNumberVisible; i++){
        oslSetBkColor(RGBA(0, 0, 0, 0));
        if (i>menu->numberOfElements-1)
            break;
        oslSetFont(fontNormal);
        xPos = menu->xPos + (menu->width - oslGetStringWidth(menu->elements[i].text)) / 2.0;
        yPos = startY + fontNormal->charHeight * count + menu->interline * count;
        if (i == menu->selected){
            if (strlen(menu->elements[i].helpText)){
                oslSetFont(fontHelp);
                oslSetTextColor(RGBA(0, 0, 0, 255));
                oslDrawString(480 / 2 - oslGetStringWidth(menu->elements[i].helpText) / 2.0, 270 - fontHelp->charHeight, menu->elements[i].helpText);
            }
            oslSetFont(fontSelected);
            oslSetTextColor(RGBA(255, 169, 65, 255));
            //Pointer:
            if (++pointer->angle == 360)
                pointer->angle = 0;
            oslDrawImageXY(pointer, xPos - pointer->sizeX, yPos + 10);
        }else{
            oslSetFont(fontNormal);
            oslSetTextColor(RGBA(255, 255, 255, 255));
        }
        oslDrawString(xPos, yPos, menu->elements[i].text);
        count++;
    }
    return 0;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// processMenuKeys: process the button pressed
// NOTE: the oslReadKeys(); must be done in the calling function.
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
int processMenuKeys(struct menuElements *menu){
    struct menuElement selected;
    
    if (osl_keys->pressed.down){
        if (menu->selected < menu->numberOfElements - 1){
            menu->selected++;
            if (menu->selected >= menu->first + menu->maxNumberVisible)
                menu->first++;
        }else{
            menu->selected = 0;
            menu->first = 0;
        }
    }else if (osl_keys->pressed.up){
        if (menu->selected > 0){
            menu->selected--;
            if (menu->selected < menu->first)
                menu->first--;
        }else{
            menu->selected = menu->numberOfElements - 1;
            menu->first = menu->selected - menu->maxNumberVisible + 1;
            if (menu->first < 0)
                menu->first = 0;
        }
    }else if (osl_keys->pressed.cross){
        selected = menu->elements[menu->selected];
        if (selected.triggerFunction != NULL)
            selected.triggerFunction();
    }else if (osl_keys->pressed.circle){
        if (menu->cancelFunction != NULL)
            menu->cancelFunction();
    }
    return 0;
}
